/**
 * author  : wangzhan
 * date    : 2015/8/3
 * version : 1.0
 */
(function($){
        if(typeof TouchSlide == 'function') {
        TouchSlide({
            slideCell:"#focus",
            titCell:".hd ul",
            mainCell:".bd ul",
            effect:"left",
            interTime:3000,
            autoPlay:true,
            autoPage:true
        });

        var baseHeight = 63,
            icont = $('#dt-icont'),
            introHeight = icont.height(),
            isIcontShow = false;

        icont.height(baseHeight);

        $('.dt-slide').on('tap',function(){
            if(!isIcontShow){
                isIcontShow = true;
                icont.height(introHeight);
            }else{
                isIcontShow = false;
                icont.height(baseHeight);
            }
        });


        var brs_height = document.documentElement.clientHeight;
        var eleTop = $('#j_comment').offset().top;

        window.onscroll = function () {
            var scrTop = document.body.scrollTop;
            var lr = parseInt(eleTop - scrTop);

            if (lr <= brs_height) {
                $('.fixed-comment').show();
            }else{
                $('.fixed-comment').hide();
            }
        };
    }
}(Zepto));

Array.prototype.indexOf = function(val) {
    for (var i = 0; i < this.length; i++) {
        if (this[i] == val) return i;
    }
    return -1;
};

Array.prototype.remove = function(val) {
    var index = this.indexOf(val);
    if (index > -1) {
        this.splice(index, 1);
    }
};

function getCookie(name) {
    var result,arr,reg=new RegExp("(^| )"+name+"=([^;]*)(;|$)");

    if(arr=document.cookie.match(reg)) {
        result = unescape(arr[2]);
    } else {
        result = null;
    }

    return result;
}

function delCookie(name) {
    var exp = new Date();
    exp.setTime(exp.getTime() - 1);
    var cval=getCookie(name);

    if(cval!=null) {
        document.cookie= name + "="+cval+";expires="+exp.toGMTString();
    }
}

function setCookie(name, value, time) {
    var strsec = getsec(time);
    var exp = new Date();

    exp.setTime(exp.getTime() + strsec*1);
    document.cookie = name + "="+ escape (value) + ";expires=" + exp.toGMTString();
}

function getsec(str) {
    var cstr;
    var str1=str.substring(1,str.length)*1;
    var str2=str.substring(0,1);

    if (str2=="s") {
        cstr = str1*1000;
    } else if (str2=="h") {
        cstr = str1*60*60*1000;
    } else if (str2=="d") {
        cstr = str1*24*60*60*1000;
    }

    return cstr;
}

$(function(){
    // toastr
    toastr.options = {
      "closeButton": false,
      "debug": false,
      "newestOnTop": false,
      "progressBar": false,
      "positionClass": "toast-bottom-full-width",
      "preventDuplicates": false,
      "onclick": null,
      "showDuration": "100",
      "hideDuration": "500",
      "timeOut": "2000",
      "extendedTimeOut": "500",
      "showEasing": "swing",
      "hideEasing": "linear",
      "showMethod": "fadeIn",
      "hideMethod": "fadeOut"
    };

    if(typeof lightbox !== 'undefined') {
      // lightbox2
      lightbox.option({
        'fitImagesInViewport': true,
        'showImageNumberLabel': false,
        'wrapAround': false,
        'alwaysShowNavOnTouchDevices': false,
        'fadeDuration': 200,
        'resizeDuration': 200,
      });
    }
});

var hasclickId=[];
$('.usefull').on('click',usefull_click);
$('.nouse').on('click',nouse_click);
function nouse_click(){
    cid=$(this).attr('data-id')
    if(hasclickId.indexOf(cid)>-1){  
       toastr['info']('亲，您已经评价过这条评论了');      
       return false;
    }else{
          returncontent=$(this).find('em')
          $.ajax({
            data:{
                cid:cid,
            },
            url:'/show/nouse', 
            type:'post',         
            success:function(res){
              if(res == -1) {
                location.href="joggers://login/";
              } else if(res == -2){
                toastr['info']('亲，您已经评价过这条评论了');
                hasclickId.push(cid);
              } else {
                returncontent.html(res);
                hasclickId.push(cid);
              }
            }
          }) 
    }    
    return false;
}
function usefull_click(){
    cid=$(this).attr('data-id')
    if(hasclickId.indexOf(cid)>-1){        
       toastr['info']('亲，您已经评价过这条评论了');
       return false;
    }else{
          returncontent=$(this).find('em');          
          $.ajax({
            data:{
                cid:cid,
            },
            url:'/show/usefull',
            type:'post',           
            success:function(res){
              if(res == -1) {
                location.href="joggers://login/";
              } else if(res == -2){
                toastr['info']('亲，您已经评价过这条评论了');
                hasclickId.push(cid);
              } else {
                returncontent.html(res);
                hasclickId.push(cid);
              }
            }
          }) 
    }    
    return false;
}